package controller.expertInfo.adminCheck;

import StaticFinal.CrudType;
import base.response.OffsetLimitPage;
import controller.common.BaseController;
import controller.common.StaticConstant;
import exception.WTException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.dictionary.CertificateTypeServices;
import services.dictionary.MajorTypeServices;
import services.expertInfo.*;
import services.specific.AdminCheckExpertServices;
import util.JsonUtil;
import util.ObjUtil;
import wt.entity.dictionary.MajorType;
import wt.entity.expertInfo.*;
import wt.staticData.TableName;
import wt.webPlugin.ZTree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller

@RequestMapping("expertInfo/adminCheck")
public class AdminCheckController extends BaseController {

    @Autowired
    ExpertBaseServices expertBaseServices;
    @Autowired
    ExpertCompanyServices expertCompanyServices;
    @Autowired
    ExpertCertificateServices expertCertificateServices;
    @Autowired
    ExpertWorkServices expertWorkServices;
    @Autowired
    ExpertEducationServices expertEducationServices;
    @Autowired
    ExpertReviewServices expertReviewServices;
    @Autowired
    ExpertTrainServices expertTrainServices;
    @Autowired
    ExpertMajorServices expertMajorServices;
    @Autowired
    LeaderCheckServices adminCheckServices;
    @Autowired
    AdminCheckExpertServices adminCheckExpertServices;
    @Autowired
    MajorTypeServices majorTypeServices;
    @Autowired
    CertificateTypeServices certificateTypeServices;

    @RequiresPermissions("expertInfo:adminCheck:default")
    @RequestMapping("index")
    public String index() {
        return "expertInfo/adminCheck/index";
    }

    @RequiresPermissions("expertInfo:adminCheck:default")
    @ResponseBody
    @RequestMapping("select")
    public OffsetLimitPage select(String expertName, String tableName, Integer limit, Integer offset) throws Exception {

        if (StringUtils.isBlank(expertName)) {
            expertName = null;
        } else {
            expertName = "%" + expertName.trim() + "%";
        }

        if (StringUtils.isBlank(tableName)) {
            tableName = null;
        } else {
            tableName = tableName.trim();
        }

        return adminCheckExpertServices.select(expertName, tableName, offset, limit);
    }

    @RequiresPermissions("expertInfo:adminCheck:default")
    @RequestMapping("showDetail")
    public String showDetail(String id, String tableName, Map map) throws Exception {

        map.put("id", id);
        map.put("tableName", tableName);

        if (TableName.EMS_EXPERT_BASE.equals(tableName)) {

            ExpertBase temp = expertBaseServices.selectByPrimaryKey(id);
            bindData(temp.getCrudType(), temp, temp.getCrudData(), map);
            return "expertInfo/adminCheck/expertBaseDlg";

        } else if (TableName.EMS_EXPERT_CERTIFICATE.equals(tableName)) {

            ExpertCertificate temp = expertCertificateServices.selectByPrimaryKey(id);
            temp.setCertificateType(certificateTypeServices.selectByPrimaryKey(temp.getCertificateTypeId()));

            bindData(temp.getCrudType(), temp, temp.getCrudData(), map);
            if (temp.getCrudType()==CrudType.update){
                ExpertCertificate ec=   (ExpertCertificate) map.get("newData");
                ec.setCertificateType(certificateTypeServices.selectByPrimaryKey(ec.getCertificateTypeId()));
            }

            return "expertInfo/adminCheck/expertCertificateDlg";

        } else if (TableName.EMS_EXPERT_COMPANY.equals(tableName)) {

            ExpertCompany temp = expertCompanyServices.selectByPrimaryKey(id);
            bindData(temp.getCrudType(), temp, temp.getCrudData(), map);
            return "expertInfo/adminCheck/expertCompanyDlg";

        } else if (TableName.EMS_EXPERT_EDUCATION.equals(tableName)) {
            ExpertEducation temp = expertEducationServices.selectByPrimaryKey(id);
            bindData(temp.getCrudType(), temp, temp.getCrudData(), map);
            return "expertInfo/adminCheck/expertEducationDlg";

        } else if (TableName.EMS_EXPERT_MAJOR.equals(tableName)) {

            ExpertMajorExample expertMajorExample = new ExpertMajorExample();
            expertMajorExample.createCriteria().andExpertIdEqualTo(id).andCrudTypeNotEqualTo(CrudType.select);
            List<ExpertMajor> expertMajorList = expertMajorServices.selectByExample(expertMajorExample);


            List<String> stringList = new ArrayList<String>();
            for (ExpertMajor expertMajor : expertMajorList) {
                stringList.add(expertMajor.getMajorId());
            }
            List<MajorType> majorTypeList = majorTypeServices.selectTreeByLeafId(stringList);

            JSONArray jsonArray = new JSONArray();
            for (MajorType majorType : majorTypeList) {
                JSONObject jsonObject = ZTree.createZTreeJSONObject(majorType.getId(), majorType.getPid(), majorType.getName());
                for (ExpertMajor expertMajor : expertMajorList) {
                    if (expertMajor.getMajorId().equals(majorType.getId())) {
                        if (expertMajor.getCrudType() == CrudType.insert) {
                            jsonObject.put(ZTree.ICON_SKIN, "add");
                        } else {
                            jsonObject.put(ZTree.ICON_SKIN, "remove");
                        }
                    }
                }
                jsonArray.add(jsonObject);
            }

            map.put("data", jsonArray.toString());
            return "expertInfo/adminCheck/expertMajorDlg";
        } else if (TableName.EMS_EXPERT_REVIEW.equals(tableName)) {

            ExpertReview temp = expertReviewServices.selectByPrimaryKey(id);
            bindData(temp.getCrudType(), temp, temp.getCrudData(), map);
            return "expertInfo/adminCheck/expertReviewDlg";

        } else if (TableName.EMS_EXPERT_TRAIN.equals(tableName)) {

            ExpertTrain temp = expertTrainServices.selectByPrimaryKey(id);
            bindData(temp.getCrudType(), temp, temp.getCrudData(), map);
            return "expertInfo/adminCheck/expertTrainDlg";

        } else if (TableName.EMS_EXPERT_WORK.equals(tableName)) {

            ExpertWork temp = expertWorkServices.selectByPrimaryKey(id);
            bindData(temp.getCrudType(), temp, temp.getCrudData(), map);
            return "expertInfo/adminCheck/expertWorkDlg";

        } else {

            map.put(StaticConstant.ERROR_MESSAGE, "出现异常数据，请刷新页面！");
            return StaticConstant.ERROR_DLG;
        }


    }


    void bindData(short crudType, Object o, String crudData, Map map) throws Exception {
        if (crudType == CrudType.update) {
            map.put("data", o);
            Object temp = JsonUtil.json2Obj(crudData, o.getClass());
            ObjUtil.insertSelectiveGETSET(temp, o);
            map.put("newData", temp);
        } else if (crudType == CrudType.insert) {
            map.put("data", o);
        } else if (crudType == CrudType.delete) {
            map.put("data", o);
        } else {
          throw  new WTException("该记录已经被审核！");
        }
        map.put("crudType", crudType);
    }


    @RequiresPermissions("expertInfo:adminCheck:default")
    @ResponseBody
    @RequestMapping("checkResult")
    public CommonResult expertBase(String id, Boolean result, String tableName, String reason) throws Exception {
        CommonResult commonResult = new CommonResult();

        System.out.println(id);
        System.out.println(tableName);
        System.out.println(result);


        if (StringUtils.isEmpty(id) || result == null || StringUtils.isEmpty(tableName)) {
            commonResult.setMessage("出现为空数据");
            return commonResult.setError();
        }
        if (result == false && StringUtils.isBlank(reason)) {
            commonResult.setMessage("驳回原因不能为空");
            return commonResult.setError();
        }


        if (TableName.EMS_EXPERT_BASE.equals(tableName)) {
            expertBaseServices.adminCheckApplyByPrimaryKey(id, result, reason);
        } else if (TableName.EMS_EXPERT_CERTIFICATE.equals(tableName)) {
            expertCertificateServices.adminCheckApplyByPrimaryKey(id, result, reason);
        } else if (TableName.EMS_EXPERT_COMPANY.equals(tableName)) {
            expertCompanyServices.adminCheckApplyByPrimaryKey(id, result, reason);
        } else if (TableName.EMS_EXPERT_EDUCATION.equals(tableName)) {
            expertEducationServices.adminCheckApplyByPrimaryKey(id, result, reason);
        } else if (TableName.EMS_EXPERT_MAJOR.equals(tableName)) {
            expertMajorServices.adminCheckApplyByPrimaryKey(id, result, reason);
        } else if (TableName.EMS_EXPERT_REVIEW.equals(tableName)) {
            expertReviewServices.adminCheckApplyByPrimaryKey(id, result, reason);
        } else if (TableName.EMS_EXPERT_TRAIN.equals(tableName)) {
            expertTrainServices.adminCheckApplyByPrimaryKey(id, result, reason);
        } else if (TableName.EMS_EXPERT_WORK.equals(tableName)) {
            expertWorkServices.adminCheckApplyByPrimaryKey(id, result, reason);
        } else {
            commonResult.setMessage("无效字段（tableName）");
            return commonResult.setError();
        }

        return commonResult.setSuccess();
    }


    @RequiresPermissions("expertInfo:adminCheck:default")
    @ResponseBody
    @RequestMapping("welcomeTips")
    public CommonResult welcomeTips() throws Exception {
        CommonResult commonResult = new CommonResult();
        OffsetLimitPage offsetLimitPage = adminCheckExpertServices.select(null, null, 0, 1);
        if (offsetLimitPage.getTotal() == 0) {
            return commonResult.setError();
        } else {
            Map map = new HashMap();
            map.put("info", offsetLimitPage.getTotal() + "条专家信息待审核");
            map.put("url", "expertInfo/adminCheck/index");
            commonResult.setData(map);
            return commonResult.setSuccess();
        }
    }

}
